using System;
using System.Data;
using System.Web;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Web.SessionState;
using System.IO;
using System.Text.RegularExpressions;

namespace AliCMS.TemplateEngine
{
    internal abstract class Tag
    {
        internal Tag(Engine engine, int index, int length, int contentIndex, int contentLength, Tag parent)
        {
            _engine = engine;
            _index = index;
            _length = length;
            _contentIndex = contentIndex;
            _contentLength = contentLength;
            _parent = parent;
            _childTags = new List<Tag>();
            _resultXml = null;
            _resultCode = null;

            #region ID
            Regex re = new Regex("^<ali.*\\s+ID=\"(?<ID>[a-zA-Z_]\\w*)\"", RegexOptions.Compiled);
            Match match = re.Match(engine.Text, _index, _length);
            if (match.Success)
                _id = match.Groups["ID"].Value;
            else
                _id = null;
            #endregion

            #region Validate
            _validate = "false";
            re = new Regex("Validate=\"(?<v>.*?)\"", RegexOptions.Compiled);
            match = re.Match(Engine.Text, _index, _length);
            if (match.Success)
                _validate = match.Groups["v"].Value.ToLower();
            #endregion
        }

        internal virtual void Init()
        {
        }
        internal virtual void Parse()
        {
        }
        internal virtual string ParseVariable(Expression variable)
        {
            return null;
        }
        internal virtual string ParseFunction(Expression variable)
        {
            return null;
        }

        #region Properties
        internal Engine Engine
        {
            get { return _engine; }
        }
        internal string Id
        {
            get { return _id; }
        }
        internal string Validate
        {
            get { return _validate; }
        }
        internal string UniqueName
        {
            get { return "t" + _index; }
        }
        internal int Index
        {
            get { return _index; }
        }
        internal int Length
        {
            get { return _length; }
        }
        internal int ContentIndex
        {
            get { return _contentIndex; }
        }
        internal int ContentLength
        {
            get { return _contentLength; }
        }
        internal Tag Parent
        {
            get { return _parent; }
        }
        internal List<Tag> ChildTags
        {
            get { return _childTags; }
        }
        internal string ResultXml
        {
            get { return _resultXml; }
            set { _resultXml = value; }
        }
        internal string ResultCode
        {
            get { return _resultCode; }
            set { _resultCode = value; }
        }
        protected IManager Manager
        {
            get
            {
                if (_manager == null)
                    return (IManager)HttpContext.Current.Application["Manager"];

                return _manager;
            }
        }
        #endregion

        #region Fields
        private Engine _engine;
        private string _id;
        private string _validate;
        private int _index;
        private int _length;
        private int _contentIndex;
        private int _contentLength;
        private Tag _parent;
        private List<Tag> _childTags;
        private string _resultXml;
        private string _resultCode;
        private IManager _manager = null;
        #endregion
    }
}
